import request from '@/utils/request'

// 用户注册
export function register(data) {
  return request({
    url: '/auth/register',
    method: 'post',
    data
  })
}

// 用户登录
export function login(data) {
  return request({
    url: '/auth/login',
    method: 'post',
    data
  })
}

// 用户登出
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/auth/userInfo',
    method: 'get'
  })
}

// 更新用户信息
export function updateUserInfo(data) {
  return request({
    url: '/user/update',
    method: 'put',
    data
  })
}

// 修改密码
export function changePassword(data) {
  return request({
    url: '/user/change-password',
    method: 'put',
    data
  })
}

// 重置密码
export function resetPassword(data) {
  return request({
    url: '/user/reset-password',
    method: 'put',
    data
  })
}

// 获取用户列表
export function getUserList(params) {
  return request({
    url: '/api/system/users',
    method: 'get',
    params
  })
}

// 获取用户详情
export function getUserDetail(userId) {
  return request({
    url: `/api/system/users/${userId}`,
    method: 'get'
  })
}

// 创建用户
export function createUser(data) {
  return request({
    url: '/api/system/users',
    method: 'post',
    data
  })
}

// 更新用户
export function updateUser(data) {
  return request({
    url: `/api/system/users/${data.id}`,
    method: 'put',
    data
  })
}

// 删除用户
export function deleteUser(userId) {
  return request({
    url: `/api/system/users/${userId}`,
    method: 'delete'
  })
}

// 批量删除用户
export function batchDeleteUsers(userIds) {
  return request({
    url: '/api/system/users/batch-delete',
    method: 'delete',
    data: {
      userIds
    }
  })
}

// 禁用/启用用户
export function toggleUserStatus(userId, data) {
  return request({
    url: `/api/system/users/${userId}/status`,
    method: 'put',
    data
  })
}

// 获取用户角色
export function getUserRoles() {
  return request({
    url: '/api/system/roles',
    method: 'get'
  })
}

// 分配用户角色
export function assignUserRoles(userId, roleIds) {
  return request({
    url: `/api/system/users/${userId}/roles`,
    method: 'put',
    data: {
      roleIds
    }
  })
}

// 检查用户名是否存在
export function checkUsernameExists(username, userId = '') {
  return request({
    url: '/api/system/users/check-username',
    method: 'get',
    params: {
      username,
      userId
    }
  })
}

// 检查手机号是否存在
export function checkPhoneExists(phone, userId = '') {
  return request({
    url: '/api/system/users/check-phone',
    method: 'get',
    params: {
      phone,
      userId
    }
  })
}

// 检查邮箱是否存在
export function checkEmailExists(email, userId = '') {
  return request({
    url: '/api/system/users/check-email',
    method: 'get',
    params: {
      email,
      userId
    }
  })
}